#ifndef __DOORBELL_SOUND_H__
#define __DOORBELL_SOUND_H__

#include "freertos/ringbuf.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_codec_dev.h"
#include "driver/i2c.h"
#include "driver/i2s_std.h"
#include "esp_codec_dev_defaults.h"

#define I2C_PORT I2C_NUM_0
#define I2C_SDA_PIN 0
#define I2C_SCL_PIN 1
#define I2S_PORT I2S_NUM_0
#define I2S_MCLK_PIN 3
#define I2S_BCLK_PIN 2
#define I2S_WS_PIN 5
#define I2S_DI_PIN 4
#define I2S_DO_PIN 6
#define PA_PIN 46
#define I2S_SAMPLE_RATE 16000
#define I2S_SAMPLE_BITS 16
#define I2S_CHANNELS I2S_SLOT_MODE_MONO

#define I2S_VOLUME 60.0
#define I2S_GAIN 30.0

#define BUFLEN (4096 - 8)

typedef struct doorbell_sound_obj {
    RingbufHandle_t mic_buffer;   // 麦克风环形缓冲区
    RingbufHandle_t speaker_buffer; // 扬声器环形缓冲区
    TaskHandle_t mic_task_handle;   // 麦克风数据拷贝任务句柄
    TaskHandle_t speaker_task_handle; // 扬声器数据拷贝任务句柄
    esp_codec_dev_handle_t codec_dev; // 音频设备句柄
} doorbell_sound_t;

typedef doorbell_sound_t *doorbell_sound_handle_t;

void doorbell_sound_init(doorbell_sound_handle_t *handle, RingbufHandle_t mic_buffer, RingbufHandle_t speaker_buffer);

void doorbell_sound_start(doorbell_sound_handle_t handle);

void doorbell_sound_stop(doorbell_sound_handle_t handle);

void doorbell_sound_deinit(doorbell_sound_handle_t handle);

#endif /* __DOORBELL_SOUND_H__ */
